# coding:utf-8

from databaseHandler import db
from datetime import datetime
from .userModel import *
from .nodeModel import *

# 论坛版块的数据库模型


class Category(db.Entity):
    categoryName = Required(str)  # 版块名称
    categoryDescription = Optional(str)  # 版块介绍
    categoryCreatedTime = Required(datetime)  # 版块创建时间
    nodes = Set('Node')  # 版块包含的节点


# 新建分类
@db_session
def newCategory(categoryName, categoryDescription):
    Category(
        categoryName=categoryName,
        categoryDescription=categoryDescription,
        categoryCreatedTime=datetime.now()
    )
    return True

# 编辑分类


@db_session
def editCategory(categoryName, categoryDescription):
    category = Category.get(categoryName=categoryName)
    category.set(categoryName=categoryName,
                 categoryDescription=categoryDescription)
    db.commit()
    return True

# 删除分类


@db_session
def deleteCategory(categoryName):
    category = Category.get(categoryName=categoryName)
    category.delete()
    db.commit()
    return True
